﻿// ----------------------------------------------------------------------------------
// Sample Code from Fernando Machado Píriz's blog: http://fernandomachadopiriz.com
//
// <copyright company="Fernando Machado Píriz" file="DesktopBackgroundViewModel.cs">
// Copyright (c) Fernando Machado Píriz. Distributed under FSF's GPL v2.</copyright>
// ----------------------------------------------------------------------------------
// This code and information are provided "as is" without warranty of any kind,
// either expressed of implied, including but not limited to the implied warranties
// of merchantability and/or fitness for a particular purpose.
// ----------------------------------------------------------------------------------
namespace DesktopBackground
{
    using System;
    using System.Collections.Generic;
    using System.Configuration;
    using System.Diagnostics.CodeAnalysis;
    using System.Globalization;
    using System.Linq;
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Input;
    using GivingAPresentation.Commons;
    using Microsoft.Win32;
    
    /// <summary>
    /// The view model of the <code>DesktopBackgroundView</code> using the Model View View Model pattern. An instance of this class is
    /// automatically created by the view as a window resource.
    /// </summary>
    public class DesktopBackgroundViewModel : BaseViewModel
    {
        /// <summary>
        /// The backend field for <code>Model</code> property.
        /// </summary>
        private DesktopBackgroundModel model = new DesktopBackgroundModel();

        /// <summary>
        /// The backend field for <code>View</code> property.
        /// </summary>
        private DesktopBackgroundView view;

        /// <summary>
        /// Path to current desktop background file.
        /// </summary>
        private string originalWallpaper;

        /// <summary>
        /// Value of the current desktop background tiled configuration (1: tiled; 0: center or fit to screen).
        /// </summary>
        private int originalTileWallpaper;

        /// <summary>
        /// Value of the current desktop background stye configuration (1: centered; 0: tiled; 2: fit to screen).
        /// </summary>
        private int originalWallpaperStyle;
        
        /// <summary>
        /// Initializes a new instance of the DesktopBackgroundViewModel class.
        /// </summary>
        public DesktopBackgroundViewModel()
        {
            this.Browse = new RelayCommand(
                argument => this.DoBrowse(),
                result => !this.model.Presenting);
        }

        /// <summary>
        /// Gets a command to display a dialog box to add one or more running programs to the list of programs to be closed while the user is
        /// giving a presentation.
        /// </summary>
        public ICommand Browse { get; private set; }

        /// <summary>
        /// Gets the model associated with this view model.
        /// </summary>
        public DesktopBackgroundModel Model
        {
            get
            {
                return this.model;
            }
        }

        /// <summary>
        /// Sets the view associated with this view model.
        /// </summary>
        /// <param name="value">The view to be associated with this view model.</param>
        public void AssignView(DesktopBackgroundView value)
        {
            this.view = value;
        }

        /// <summary>
        /// Closes or terminates the selected programs if the check box checkBoxCloseThesePrograms is checked.
        /// </summary>
        public void DoStartPresentation()
        {
            if (this.model.Presenting)
            {
                return;
            }

            this.model.Presenting = true;
            this.view.IsEnabled = false;

            if (this.model.Replace || this.model.Remove)
            {
                this.originalWallpaper = DesktopBackgroundHelper.Wallpaper;
                this.originalTileWallpaper = DesktopBackgroundHelper.TileWallpaper;
                this.originalWallpaperStyle = DesktopBackgroundHelper.WallpaperStyle;
            }

            if (this.model.Remove)
            {
                DesktopBackgroundHelper.Wallpaper = String.Empty;
            }
            else if (this.model.Replace)
            {
                DesktopBackgroundHelper.TileWallpaper = this.model.TileWallpaper;
                DesktopBackgroundHelper.WallpaperStyle = this.model.WallpaperStyle;
                DesktopBackgroundHelper.Wallpaper = this.model.Wallpaper;
            }
        }

        /// <summary>
        /// Starts the selected programs if the check box checkBoxCloseThesePrograms is checked.
        /// </summary>
        public void DoStopPresentation()
        {
            if (!this.model.Presenting)
            {
                return;   
            }

            if (this.model.Replace || this.model.Remove)
            {
                DesktopBackgroundHelper.TileWallpaper = this.originalTileWallpaper;
                DesktopBackgroundHelper.WallpaperStyle = this.originalWallpaperStyle;
                DesktopBackgroundHelper.Wallpaper = this.originalWallpaper;
            }

            this.model.Presenting = false;
            this.view.IsEnabled = true;
        }

        /// <summary>
        /// Loads model state.
        /// </summary>
        [SuppressMessage("Microsoft.Globalization", "CA1303:Do not pass literals as localized parameters",
            MessageId = "GivingAPresentation.Commons.ExceptionHelper.Check(System.Boolean,System.String)",
            Justification = "Resources will be used in a future version.")]
        public void DoInitializePart()
        {
            ExceptionHelper.Check(!this.model.Presenting, "Cannot load settings while giving a presentation.");

            try
            {
                this.model.Load();
            }
            catch (ConfigurationErrorsException e)
            {
                throw new GivingAPresentationException("Cannot load configuration.", e);
            }
        }

        /// <summary>
        /// Stores model state.
        /// </summary>                                                                   
        [SuppressMessage("Microsoft.Globalization", "CA1303:Do not pass literals as localized parameters",
            MessageId = "GivingAPresentation.Commons.ExceptionHelper.Check(System.Boolean,System.String)",
            Justification = "Resources will be used in a future version.")]
        public void DoFinalizePart()
        {
            ExceptionHelper.Check(!this.model.Presenting, "Cannot save settings while giving a presentation.");

            try
            {
                this.model.Save();
            }
            catch (ConfigurationErrorsException e)
            {
                throw new GivingAPresentationException("Cannot save configuration.", e);
            }
        }

        /// <summary>
        /// Opens a dialog box for the user to select an image to be used as background while giving a presentation.
        /// </summary>
        public void DoBrowse()
        {
            OpenFileDialog dialog = new Microsoft.Win32.OpenFileDialog();
            dialog.Filter = "All image files (*.bmp;*.jpg;*.jpeg;*.dib)|*.bmp;*.jpg;*.jpeg;*.dib";
            dialog.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.CommonPictures);

            Nullable<bool> result = dialog.ShowDialog();
            if (result == true)
            {
                this.model.Wallpaper = dialog.FileName;
            }
        }
    }
}
